Person.init   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 12
rs 9.4285
1
var GedcomX = require('../'),
2
    utils = require('../utils');
3
    
4
/**
5
 * A person.
6
 * 
7
 * @see {@link https://github.com/FamilySearch/gedcomx/blob/master/specifications/json-format-specification.md#person|GEDCOM X JSON Spec}
8
 * 
9
 * @class
10
 * @extends Subject
11
 * @param {Object} [json]
12
 */
13
var Person = function(json){
14
  
15
  // Protect against forgetting the new keyword when calling the constructor
16
  if(!(this instanceof Person)){
17
    return new Person(json);
18
  }
19
  
20
  // If the given object is already an instance then just return it. DON'T copy it.
21
  if(Person.isInstance(json)){
22
    return json;
23
  }
24
  
25
  this.init(json);
0 ignored issues
show
Best Practice introduced by
There is no return statement in this branch, but you do return something in other branches. Did you maybe miss it? If you do not want to return anything, consider adding return undefined; explicitly.
Loading history...
26
};
27
28
Person.prototype = Object.create(GedcomX.Subject.prototype);
29
30
Person._gedxClass = Person.prototype._gedxClass = 'GedcomX.Person';
31
32
Person.jsonProps = [
33
  'private',
34
  'gender',
35
  'names',
36
  'facts'
37
];
38
39
/**
40
 * Check whether the given object is an instance of this class.
41
 * 
42
 * @param {Object} obj
43
 * @returns {Boolean}
44
 */
45
Person.isInstance = function(obj){
46
  return utils.isInstance(obj, this._gedxClass);
47
};
48
49
/**
50
 * Initialize from JSON
51
 * 
52
 * @param {Object}
0 ignored issues
show
Documentation introduced by
The parameter * does not exist. Did you maybe forget to remove this comment?
Loading history...
53
 * @return {Person} this
54
 */
55
Person.prototype.init = function(json){
56
  
57
  GedcomX.Subject.prototype.init.call(this, json);
58
  
59
  if(json){
60
    this.setPrivate(json.private);
61
    this.setGender(json.gender);
62
    this.setNames(json.names);
63
    this.setFacts(json.facts);
64
  }
65
  return this;
66
};
67
68
/**
69
 * Get the private flag
70
 * 
71
 * @returns {Boolean} private
72
 */
73
Person.prototype.getPrivate = function(){
74
  return this.private;
75
};
76
77
/**
78
 * Set the private flag
79
 * 
80
 * @param {Boolean} isPrivate
81
 * @returns {Person} This instance
82
 */
83
Person.prototype.setPrivate = function(isPrivate){
84
  this.private = isPrivate;
85
  return this;
86
};
87
88
/**
89
 * Get the person's gender
90
 * 
91
 * @returns {Gender} gender
92
 */
93
Person.prototype.getGender = function(){
94
  return this.gender;
95
};
96
97
/**
98
 * Set the person's gender
99
 * 
100
 * @param {Gender} gender
101
 * @returns {Person} This instance
102
 */
103
Person.prototype.setGender = function(gender){
104
  if(gender){
105
    this.gender = GedcomX.Gender(gender);
106
  }
107
  return this;
108
};
109
110
/**
111
 * Check if the person's gender is male.
112
 * 
113
 * @returns {Boolean}
114
 */
115
Person.prototype.isMale = function(){
116
  return this.gender && this.gender.getType() === 'http://gedcomx.org/Male';
117
};
118
119
/**
120
 * Check if the person's gender is female.
121
 * 
122
 * @returns {Boolean}
123
 */
124
Person.prototype.isFemale = function(){
125
  return this.gender && this.gender.getType() === 'http://gedcomx.org/Female';
126
};
127
128
/**
129
 * Get the names
130
 * 
131
 * @return {Name[]}
132
 */
133
Person.prototype.getNames = function(){
134
  return this.names || [];
135
};
136
137
/**
138
 * Set the names
139
 * 
140
 * @param {Name[]|Object[]} names
141
 * @returns {Person} This instance
142
 */
143
Person.prototype.setNames = function(names){
144
  return this._setArray(names, 'names', 'addName');
145
};
146
147
/**
148
 * Add a name
149
 * 
150
 * @param {NameForm|Object} name
151
 * @returns {Person} This instance
152
 */
153
Person.prototype.addName = function(name){
154
  return this._arrayPush(name, 'names', GedcomX.Name);
155
};
156
157
/**
158
 * Get the facts
159
 * 
160
 * @return {Fact[]}
161
 */
162
Person.prototype.getFacts = function(){
163
  return this.facts || [];
164
};
165
166
/**
167
 * Set the facts
168
 * 
169
 * @param {Fact[]|Object[]} facts
170
 * @returns {Person} This instance
171
 */
172
Person.prototype.setFacts = function(facts){
173
  return this._setArray(facts, 'facts', 'addFact');
174
};
175
176
/**
177
 * Get facts matching the given type
178
 * 
179
 * @param {String} type Fact type
180
 * @return {Fact[]}
181
 */
182
Person.prototype.getFactsByType = function(type){
183
  return this.getFacts().filter(function(f){
184
    return f.getType() === type;
185
  });
186
};
187
188
/**
189
 * Add a fact
190
 * 
191
 * @param {Fact|Object} fact
192
 * @returns {Person} This instance
193
 */
194
Person.prototype.addFact = function(fact){
195
  return this._arrayPush(fact, 'facts', GedcomX.Fact);
196
};
197
198
/**
199
 * Export the object as JSON
200
 * 
201
 * @return {Object} JSON object
202
 */
203
Person.prototype.toJSON = function(){
204
  return this._toJSON(GedcomX.Subject, Person.jsonProps);
205
};
206
207
module.exports = Person;